/**
 * Created by zahir on 2015/4/2.
 */
var Q = require('q');
var models = require('../models');
var FileModel = models.FileModel;


function FileDao() {

}

FileDao.prototype.insertFile = function (file) {
    var defferred = Q.defer();
    var fileModel = new FileModel();

    fileModel.FFILENAME = file.fileName;
    fileModel.FFILERENAME = file.fileRename;
    fileModel.FPARSERTYPE = file.parserType;

    fileModel.save(function (err, data) {
        if (err) {
            defferred.reject(err);
        } else {
            defferred.resolve(data);
        }
    });
    return defferred.promise;
}

FileDao.prototype.findUnCheckFileInfo = function(){
    var defferred = Q.defer();
    FileModel.find({'FFILESTATUS':0},function(err,data){
        if(err){
            defferred.reject(err);
        }else{
            defferred.resolve(data);
        }
    });
    return defferred.promise;
}


FileDao.prototype.findFileListByStatus = function(option){
    var defferred = Q.defer();
    FileModel.find({'FFILESTATUS':option.status},{},{sort:'-FUploadTime',limit:option.pageSize,skip:option.startAt},function(err,data){
        if(err){
            defferred.reject(err);
        }else{
            defferred.resolve(data);
        }
    });
    return defferred.promise;
}

FileDao.prototype.updateCheckedOfFile = function (id,status) {
    var defferred = Q.defer();
    FileModel.update({_id:id},{$set:{'FFILESTATUS':status}},function (err, data) {
        if (err) {
            defferred.reject(err);
        } else {
            defferred.resolve(data);
        }
    });
    return defferred.promise;
}

FileDao.prototype.insertErrorMessage = function (id,error) {
    FileModel.update({_id:id},{$set:{'FERRORMESSAGE':error}},function (err, data) {

    });
}

FileDao.prototype.deleteFile = function(fileID){
    FileModel.findOneAndRemove({_id:fileID},function(err,data){
        console.log(err,data);
    });
}

exports.FileDao = FileDao;





